package com.wawayaya.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

import com.wawayaya.bean.Download;
import com.wawayaya.service.download.BookItemVessel;
import com.wawayaya.util.Constants;
import com.wawayaya.util.TLog;

import java.util.LinkedList;

/**
 * 下载服务
 */
public class DownloadService extends Service {

    private static final String TAG = DownloadService.class.getSimpleName();
    private BookItemVessel bookItemVessel;

    String uid="";
    String token="";
    String nickname ="";
    Context context;
    int   flag = 2;
    long  bookIdLocal=0L;
    @Override
    public void onCreate() {
        super.onCreate();
        this.context =this;
        //bookItemVessel = BookItemVessel.getInstance(this);
    }

    @Override
    public void onDestroy() {
        Log.e("DownloadService", "返回数据。。。。DownloadS  停止服务   ervice..bookDownload。。");
        super.onDestroy();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if(null!=intent){
            if(intent.getFlags()== Constants.bookDownload){
                TLog.log("。。。。DownloadService..bookDownload。。");
                bookDownload();
            }
            if(intent.getFlags()== Constants.supportDownload){
                TLog.log("。。。。DownloadService..supportDownload。。");
                supportDownload();
            }
        }

        return super.onStartCommand(intent, flags, startId);
    }

    public  void   bookDownload() {
        LinkedList<Download> tasks =  bookItemVessel.getTasks();
        Log.d(TAG, "....start book download..." + tasks.size());
        if (tasks.size() > 0 ) {
            try {
                final Download  download = tasks.getFirst();
                Log.d(TAG, "....start book download..." + tasks.size() + "url:" + download.book.getPartArray());
                download.downloadTask.initTaskInfo(download);
                download.downloadTask.execute();
            } catch (IllegalStateException ise) {
                ise.printStackTrace();
            }
        }
    }

    /**
     * 判断本地是否已经下载，下载后则不启动下载
     */
    public  void  supportDownload(){
      /*  LinkedList<MySupportInfo> supTasks = bookItemVessel.getSupportTasks();
        // Log.d(TAG, "start support Download size:"+supTasks.size());
        if(supTasks.size()>0){
            try{
                MySupportInfo supInfo= supTasks.getFirst();
                String sfolder = utils.getireadckclocalstrokedatafolder(true, null);

                supInfo.getSupportTask().initTaskInfo(supInfo.getSupportFile(),sfolder,sfolder+ File.separator+supInfo.getSupportFile().getSfKey()+".zip",supInfo.getCallback());

                supInfo.getSupportTask().execute();
            }catch(Exception e){
                e.printStackTrace();
            }
        }*/
    }
}
